<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>并发服务器 - lclei blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="lclei" /><meta name="description" content=" fork进程 IO复用 线程   多进程 主进程监听，在循环中接受连接请求，当连接建立后，fork一个子进程，在子进程中进行处理。
  主进程：listen -&amp;gt; while(1) -&amp;gt; accept -&amp;gt; fork -&amp;gt; close connfd
  子进程：close listenfd -&amp;gt; handle -&amp;gt; close connfd
 " /><meta name="keywords" content="lclei, blog, code" />






<meta name="generator" content="Hugo 0.68.3 with theme even" />


<link rel="canonical" href="http://localhost:1313/post/wbcurrency/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.2e81bbed97b8b282c1aeb57488cc71c8d8c8ec559f3931531bd396bf31e0d4dd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="并发服务器" />
<meta property="og:description" content="
fork进程
IO复用
线程


多进程
主进程监听，在循环中接受连接请求，当连接建立后，fork一个子进程，在子进程中进行处理。


主进程：listen -&gt; while(1) -&gt; accept -&gt; fork -&gt; close connfd


子进程：close listenfd -&gt; handle -&gt; close connfd

" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://localhost:1313/post/wbcurrency/" />
<meta property="article:published_time" content="2019-12-10T00:00:00+00:00" />
<meta property="article:modified_time" content="2019-12-10T00:00:00+00:00" />
<meta itemprop="name" content="并发服务器">
<meta itemprop="description" content="
fork进程
IO复用
线程


多进程
主进程监听，在循环中接受连接请求，当连接建立后，fork一个子进程，在子进程中进行处理。


主进程：listen -&gt; while(1) -&gt; accept -&gt; fork -&gt; close connfd


子进程：close listenfd -&gt; handle -&gt; close connfd

">
<meta itemprop="datePublished" content="2019-12-10T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2019-12-10T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="2068">



<meta itemprop="keywords" content="linux,net," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="并发服务器"/>
<meta name="twitter:description" content="
fork进程
IO复用
线程


多进程
主进程监听，在循环中接受连接请求，当连接建立后，fork一个子进程，在子进程中进行处理。


主进程：listen -&gt; while(1) -&gt; accept -&gt; fork -&gt; close connfd


子进程：close listenfd -&gt; handle -&gt; close connfd

"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">lclei</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">lclei</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">并发服务器</h1>

      <div class="post-meta">
        <span class="post-time"> 2019-12-10 </span>
        
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">Contents</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#多进程">多进程</a></li>
        <li><a href="#io复用">IO复用</a>
          <ul>
            <li><a href="#stdio与connfd的复用">stdio与connfd的复用</a></li>
            <li><a href="#基于io多路复用的并发事件驱动器">基于I/O多路复用的并发事件驱动器</a></li>
          </ul>
        </li>
        <li><a href="#多线程">多线程</a></li>
        <li><a href="#基于线程的事件驱动程序">基于线程的事件驱动程序</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <ol>
<li>fork进程</li>
<li>IO复用</li>
<li>线程</li>
</ol>
<hr>
<h2 id="多进程">多进程</h2>
<p>主进程监听，在循环中接受连接请求，当连接建立后，fork一个子进程，在子进程中进行处理。</p>
<ul>
<li>
<p>主进程：listen -&gt; while(1) -&gt; accept -&gt; fork -&gt; close connfd</p>
</li>
<li>
<p>子进程：close listenfd -&gt; handle -&gt; close connfd</p>
</li>
</ul>
<p>server:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoserverfork 6667
Parent: waitting&hellip;
name[localhost] port[38276]connected! Child No.1 will handle it!
Parent: waitting&hellip;
server recived 6 bytes
name[localhost] port[38280]connected! Child No.2 will handle it!
Parent: waitting&hellip;
server recived 7 bytes</p>
</blockquote>
<p>client1:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
first
first</p>
</blockquote>
<p>client2:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
second
second</p>
</blockquote>
<p>code</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#include</span> <span class="cpf">&#34;mynet.h&#34;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">open_listenfd</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">port</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">echo</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">sigchld_handler</span><span class="p">(</span><span class="kt">int</span> <span class="n">sig</span><span class="p">)</span>
<span class="p">{</span>
	<span class="k">while</span><span class="p">(</span><span class="n">waitpid</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">WNOHANG</span><span class="p">)</span> <span class="o">&gt;</span><span class="mi">0</span><span class="p">)</span>
	  <span class="p">;</span>
	<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="n">connfd</span><span class="p">,</span> <span class="n">cno</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
	<span class="n">socklen_t</span> <span class="n">clientlen</span><span class="p">;</span>
	<span class="k">struct</span> <span class="n">sockaddr_storage</span> <span class="n">clientaddr</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">client_hostname</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">],</span> <span class="n">client_port</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>

	<span class="k">if</span><span class="p">(</span><span class="n">argc</span><span class="o">!=</span><span class="mi">2</span><span class="p">){</span>
	  <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&#34;usage: %s &lt;port&gt;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
	  <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">signal</span><span class="p">(</span><span class="n">SIGCHLD</span><span class="p">,</span> <span class="n">sigchld_handler</span><span class="p">);</span>
	<span class="n">listenfd</span> <span class="o">=</span> <span class="n">open_listenfd</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
	<span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;Parent: waitting...</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
		<span class="n">clientlen</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_storage</span><span class="p">);</span>
		<span class="n">connfd</span> <span class="o">=</span> <span class="n">accept</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span> <span class="p">(</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="o">&amp;</span><span class="n">clientlen</span><span class="p">);</span>
        <span class="n">getnameinfo</span><span class="p">((</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="n">clientlen</span><span class="p">,</span><span class="n">client_hostname</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="n">client_port</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;name[%s] port[%s]connected! Child No.%d will handle it!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span>
					<span class="n">client_hostname</span><span class="p">,</span><span class="n">client_port</span><span class="p">,</span><span class="n">cno</span><span class="o">++</span><span class="p">);</span>
		<span class="k">if</span><span class="p">(</span><span class="n">fork</span><span class="p">()</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span> <span class="c1">//child
</span><span class="c1"></span>		<span class="p">{</span>
			<span class="n">close</span><span class="p">(</span><span class="n">listenfd</span><span class="p">);</span>
			<span class="n">echo</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
			<span class="n">close</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
			<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
		<span class="p">}</span>
		<span class="n">close</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">echo</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">size_t</span> <span class="n">n</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>

	<span class="k">while</span><span class="p">((</span><span class="n">n</span><span class="o">=</span><span class="n">read</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">))</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;server recived %d bytes</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">n</span><span class="p">);</span>
		<span class="n">write</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
	<span class="p">}</span>
<span class="p">}</span>
<span class="err">©</span> <span class="mi">2019</span> <span class="n">GitHub</span><span class="p">,</span> <span class="n">Inc</span><span class="p">.</span>
</code></pre></td></tr></table>
</div>
</div><hr>
<h2 id="io复用">IO复用</h2>
<p>IO复用即使用select函数</p>
<blockquote>
<p>#include &lt;sys/select.h&gt;
int select(int n, fd_set *fdset, NULL, NULL, NULL) //等待一组描述符准备好读
FD_ZERO(fd_set *fdset);
FD_CLR(int fd, fd_set *fdset);
FD_SET(int fd, fd_set *fdset);
FD_ISSET(int fd, fd_set *fdset);</p>
</blockquote>
<h3 id="stdio与connfd的复用">stdio与connfd的复用</h3>
<p>如果使用echoserver迭代版本的同时，要求能够响应服务器本身的stdin输入，则可以使用select。一个标准输入的fd一般是0，监听fd则是系统分配</p>
<ul>
<li>将stdin和llistenfd加入fdset</li>
<li>使用select等待是否有fd准备好</li>
<li>通过FD_ISSET查看哪个准备好并进行相应的处理</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c">
<span class="cp">#include</span> <span class="cpf">&#34;mynet.h&#34;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">open_listenfd</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">port</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">echo</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">command</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="n">connfd</span><span class="p">;</span>
	<span class="n">socklen_t</span> <span class="n">clientlen</span><span class="p">;</span>
	<span class="k">struct</span> <span class="n">sockaddr_storage</span> <span class="n">clientaddr</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">client_hostname</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">],</span> <span class="n">client_port</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>
	<span class="n">fd_set</span> <span class="n">read_set</span><span class="p">,</span> <span class="n">ready_set</span><span class="p">;</span>

	<span class="k">if</span><span class="p">(</span><span class="n">argc</span><span class="o">!=</span><span class="mi">2</span><span class="p">){</span>
	  <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&#34;usage: %s &lt;port&gt;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
	  <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">listenfd</span> <span class="o">=</span> <span class="n">open_listenfd</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
	<span class="n">FD_ZERO</span><span class="p">(</span><span class="o">&amp;</span><span class="n">read_set</span><span class="p">);</span>	<span class="c1">//clear read_set
</span><span class="c1"></span>	<span class="n">FD_SET</span><span class="p">(</span><span class="n">STDIN_FILENO</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">read_set</span><span class="p">);</span>	<span class="c1">//add stdin to read_set
</span><span class="c1"></span>	<span class="n">FD_SET</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">read_set</span><span class="p">);</span>	<span class="c1">//add listenfd to read_set
</span><span class="c1"></span>
	<span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
		<span class="n">ready_set</span> <span class="o">=</span> <span class="n">read_set</span><span class="p">;</span>
		<span class="n">select</span><span class="p">(</span><span class="n">listenfd</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="o">&amp;</span><span class="n">ready_set</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
		<span class="k">if</span><span class="p">(</span><span class="n">FD_ISSET</span><span class="p">(</span><span class="n">STDIN_FILENO</span><span class="p">,</span><span class="o">&amp;</span><span class="n">ready_set</span><span class="p">))</span>
		<span class="p">{</span>
			<span class="n">printf</span><span class="p">(</span><span class="s">&#34;stdin is ready.</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
			<span class="n">command</span><span class="p">();</span>
		<span class="p">}</span>
		<span class="k">if</span><span class="p">(</span><span class="n">FD_ISSET</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">ready_set</span><span class="p">)){</span>
			<span class="n">printf</span><span class="p">(</span><span class="s">&#34;client connetted.</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
			<span class="n">clientlen</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_storage</span><span class="p">);</span>
			<span class="n">connfd</span> <span class="o">=</span> <span class="n">accept</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span> <span class="p">(</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="o">&amp;</span><span class="n">clientlen</span><span class="p">);</span>
			<span class="n">getnameinfo</span><span class="p">((</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="n">clientlen</span><span class="p">,</span><span class="n">client_hostname</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="n">client_port</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
			<span class="n">echo</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
			<span class="n">close</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
		<span class="p">}</span>
	<span class="p">}</span>
	<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">command</span><span class="p">(</span><span class="kt">void</span><span class="p">){</span>
	<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>
	<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">fgets</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="n">stdin</span><span class="p">)){</span>
		<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>	<span class="c1">//EOF
</span><span class="c1"></span>	<span class="p">}</span>
	<span class="n">printf</span><span class="p">(</span><span class="s">&#34;%s&#34;</span><span class="p">,</span><span class="n">buf</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="nf">echo</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">size_t</span> <span class="n">n</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>

	<span class="k">while</span><span class="p">((</span><span class="n">n</span><span class="o">=</span><span class="n">read</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">))</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;server recived %d bytes</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">n</span><span class="p">);</span>
		<span class="n">write</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>server behavior:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./select 6667
do something
stdin is ready.
do something
hello
stdin is ready.
hello
client connetted.
server recived 12 bytes
ccd  //no response here becuse client is connecting
stdin is ready.  //client close now
ccd
f
stdin is ready.
f
stdin is ready.  //ctrl+D here</p>
</blockquote>
<p>client behavior:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
i am client
i am client</p>
</blockquote>
<h3 id="基于io多路复用的并发事件驱动器">基于I/O多路复用的并发事件驱动器</h3>
<p>与之前类似，维护一个结构体pool，存储readset，readyset和clientfd</p>
<ul>
<li>server监听，初始化pool</li>
<li>进入循环，初始化readyset，select阻塞，等待fd准备好</li>
<li>若listenfd，添加client</li>
<li>对所有准备好的clientfd回射服务</li>
</ul>
<p>server:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoserverselect 6667
localhost:38334 connected!
Server recived 5 (5 total) bytes on fd 4
Server recived 4 (9 total) bytes on fd 4
Server recived 7 (16 total) bytes on fd 4
localhost:38338 connected!
Server recived 4 (20 total) bytes on fd 5
Server recived 6 (26 total) bytes on fd 5
Server recived 2 (28 total) bytes on fd 5
Close connfd 4
Server recived 7 (35 total) bytes on fd 5
Close connfd 5</p>
</blockquote>
<p>client1:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
1234
1234
123
123
123456
123456
[root@linuxkit-00155ddc0103 inet]#</p>
</blockquote>
<p>client2:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
123
123
12345
12345
1
1
123456
123456
[root@linuxkit-00155ddc0103 inet]#</p>
</blockquote>
<p>code</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#include</span> <span class="cpf">&#34;mynet.h&#34;</span><span class="cp">
</span><span class="cp"></span><span class="k">typedef</span> <span class="k">struct</span><span class="p">{</span>
	<span class="kt">int</span> <span class="n">maxfd</span><span class="p">;</span>
	<span class="n">fd_set</span> <span class="n">read_set</span><span class="p">;</span>
	<span class="n">fd_set</span> <span class="n">ready_set</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">nready</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">maxi</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">clientfd</span><span class="p">[</span><span class="n">FD_SETSIZE</span><span class="p">];</span>
<span class="p">}</span><span class="n">pool</span><span class="p">;</span>
<span class="kt">void</span> <span class="nf">add_client</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">,</span> <span class="n">pool</span> <span class="o">*</span><span class="n">p</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">check_clients</span><span class="p">(</span><span class="n">pool</span> <span class="o">*</span><span class="n">p</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">init_pool</span><span class="p">(</span><span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="n">pool</span> <span class="o">*</span><span class="n">p</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">open_listenfd</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">port</span><span class="p">);</span>

<span class="kt">int</span> <span class="n">byte_cnt</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="n">connfd</span><span class="p">;</span>
	<span class="n">socklen_t</span> <span class="n">clientlen</span><span class="p">;</span>
	<span class="k">struct</span> <span class="n">sockaddr_storage</span> <span class="n">clientaddr</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">client_hostname</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">],</span> <span class="n">client_port</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>
	<span class="k">static</span> <span class="n">pool</span> <span class="n">pool</span><span class="p">;</span>

	<span class="k">if</span><span class="p">(</span><span class="n">argc</span><span class="o">!=</span><span class="mi">2</span><span class="p">){</span>
	  <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&#34;usage: %s &lt;port&gt;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
	  <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">listenfd</span> <span class="o">=</span> <span class="n">open_listenfd</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
	<span class="n">init_pool</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">pool</span><span class="p">);</span>

	<span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
		<span class="n">pool</span><span class="p">.</span><span class="n">ready_set</span> <span class="o">=</span> <span class="n">pool</span><span class="p">.</span><span class="n">read_set</span><span class="p">;</span>
		<span class="n">pool</span><span class="p">.</span><span class="n">nready</span> <span class="o">=</span> <span class="n">select</span><span class="p">(</span><span class="n">pool</span><span class="p">.</span><span class="n">maxfd</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="o">&amp;</span><span class="n">pool</span><span class="p">.</span><span class="n">ready_set</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
		<span class="k">if</span><span class="p">(</span><span class="n">FD_ISSET</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">pool</span><span class="p">.</span><span class="n">ready_set</span><span class="p">))</span>
		<span class="p">{</span>
			<span class="n">clientlen</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_storage</span><span class="p">);</span>
			<span class="n">connfd</span> <span class="o">=</span> <span class="n">accept</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span> <span class="p">(</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="o">&amp;</span><span class="n">clientlen</span><span class="p">);</span>
			<span class="n">getnameinfo</span><span class="p">((</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="n">clientlen</span><span class="p">,</span><span class="n">client_hostname</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="n">client_port</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
			<span class="n">printf</span><span class="p">(</span><span class="s">&#34;%s:%s connected!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">client_hostname</span><span class="p">,</span><span class="n">client_port</span><span class="p">);</span>
			<span class="n">add_client</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pool</span><span class="p">);</span>
		<span class="p">}</span>
		<span class="n">check_clients</span><span class="p">(</span><span class="o">&amp;</span><span class="n">pool</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">init_pool</span><span class="p">(</span><span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="n">pool</span> <span class="o">*</span><span class="n">p</span><span class="p">){</span>
	<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
	<span class="n">p</span><span class="o">-&gt;</span><span class="n">maxi</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
	<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">FD_SETSIZE</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
	  <span class="n">p</span><span class="o">-&gt;</span><span class="n">clientfd</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>

	<span class="n">p</span><span class="o">-&gt;</span><span class="n">maxfd</span> <span class="o">=</span> <span class="n">listenfd</span><span class="p">;</span>
	<span class="n">FD_ZERO</span><span class="p">(</span><span class="o">&amp;</span><span class="n">p</span> <span class="o">-&gt;</span> <span class="n">read_set</span><span class="p">);</span>
	<span class="n">FD_SET</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">read_set</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">add_client</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">,</span> <span class="n">pool</span> <span class="o">*</span><span class="n">p</span><span class="p">){</span>
	<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
	<span class="n">p</span><span class="o">-&gt;</span><span class="n">nready</span><span class="o">--</span><span class="p">;</span>
	<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">FD_SETSIZE</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
		<span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">clientfd</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">){</span>
			<span class="n">p</span><span class="o">-&gt;</span><span class="n">clientfd</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">connfd</span><span class="p">;</span>
			<span class="n">FD_SET</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">read_set</span><span class="p">);</span>
			<span class="k">if</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">maxfd</span><span class="p">)</span>
			  <span class="n">p</span><span class="o">-&gt;</span><span class="n">maxfd</span> <span class="o">=</span> <span class="n">connfd</span><span class="p">;</span>
			<span class="k">if</span><span class="p">(</span><span class="n">i</span><span class="o">&gt;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">maxi</span><span class="p">)</span>
			  <span class="n">p</span><span class="o">-&gt;</span><span class="n">maxi</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span>
			<span class="k">break</span><span class="p">;</span>
		<span class="p">}</span>
		<span class="k">if</span><span class="p">(</span><span class="n">i</span> <span class="o">==</span> <span class="n">FD_SETSIZE</span><span class="p">)</span>
		  <span class="n">printf</span><span class="p">(</span><span class="s">&#34;Add client error! Beyond max client numbers.</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
	<span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">check_clients</span><span class="p">(</span><span class="n">pool</span> <span class="o">*</span><span class="n">p</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">i</span><span class="p">,</span><span class="n">connfd</span><span class="p">,</span><span class="n">n</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>
	<span class="kt">char</span> <span class="n">echostr</span><span class="p">[</span><span class="n">MAXLINE</span><span class="o">+</span><span class="mi">10</span><span class="p">];</span>

	<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;(</span><span class="n">i</span><span class="o">&lt;=</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">maxi</span><span class="p">)</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">nready</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">);</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
		<span class="n">connfd</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">clientfd</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
		<span class="n">sprintf</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="s">&#34;</span><span class="se">\0</span><span class="s">&#34;</span><span class="p">);</span>

		<span class="k">if</span><span class="p">((</span><span class="n">connfd</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">)</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">FD_ISSET</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">ready_set</span><span class="p">))){</span>
			<span class="n">p</span><span class="o">-&gt;</span><span class="n">nready</span><span class="o">--</span><span class="p">;</span>
			<span class="k">if</span><span class="p">((</span><span class="n">n</span><span class="o">=</span><span class="n">read</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">))</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
				<span class="n">byte_cnt</span><span class="o">+=</span><span class="n">n</span><span class="p">;</span>
				<span class="n">printf</span><span class="p">(</span><span class="s">&#34;Server recived %d (%d total) bytes on fd %d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">n</span><span class="p">,</span><span class="n">byte_cnt</span><span class="p">,</span><span class="n">connfd</span><span class="p">);</span>
				<span class="n">write</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
			<span class="p">}</span>
			<span class="k">else</span><span class="p">{</span>
				<span class="n">printf</span><span class="p">(</span><span class="s">&#34;Close connfd %d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">connfd</span><span class="p">);</span>
				<span class="n">close</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
				<span class="n">FD_CLR</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="o">&amp;</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">read_set</span><span class="p">);</span>
				<span class="n">p</span><span class="o">-&gt;</span><span class="n">clientfd</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
			<span class="p">}</span>
		<span class="p">}</span>
	<span class="p">}</span>
<span class="p">}</span>

</code></pre></td></tr></table>
</div>
</div><hr>
<h2 id="多线程">多线程</h2>
<p>server:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoserverthread 6667
name[localhost] port[38362]connected! Thread id:140562122241792 will serve it!
name[localhost] port[38366]connected! Thread id:140562113849088 will serve it!
server recived 6 bytes from fd 4
server recived 4 bytes from fd 4
server recived 7 bytes from fd 5
server recived 4 bytes from fd 5</p>
</blockquote>
<p>client1:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667<br>
12345                                                        <br>
12345                                                        <br>
123                                                          <br>
123</p>
</blockquote>
<p>client2:</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667  <br>
654321                                                          <br>
654321                                                          <br>
321                                                             <br>
321</p>
</blockquote>
<p>注意传递connfd时是新申请空间，并在thread里释放
code</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#include</span> <span class="cpf">&#34;mynet.h&#34;</span><span class="cp">
</span><span class="cp"></span><span class="kt">int</span> <span class="nf">open_listenfd</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">port</span><span class="p">);</span>
<span class="kt">void</span> <span class="o">*</span><span class="nf">thread</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">vargp</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">echo</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">);</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="o">*</span><span class="n">connfdp</span><span class="p">,</span> <span class="n">cno</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
	<span class="n">socklen_t</span> <span class="n">clientlen</span><span class="p">;</span>
	<span class="n">pthread_t</span> <span class="n">tid</span><span class="p">;</span>
	<span class="k">struct</span> <span class="n">sockaddr_storage</span> <span class="n">clientaddr</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">client_hostname</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">],</span> <span class="n">client_port</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>

	<span class="k">if</span><span class="p">(</span><span class="n">argc</span><span class="o">!=</span><span class="mi">2</span><span class="p">){</span>
	  <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&#34;usage: %s &lt;port&gt;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
	  <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">listenfd</span> <span class="o">=</span> <span class="n">open_listenfd</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
	<span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
		<span class="n">clientlen</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_storage</span><span class="p">);</span>
		<span class="n">connfdp</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
		<span class="o">*</span><span class="n">connfdp</span> <span class="o">=</span> <span class="n">accept</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span> <span class="p">(</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="o">&amp;</span><span class="n">clientlen</span><span class="p">);</span>
        <span class="n">getnameinfo</span><span class="p">((</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="n">clientlen</span><span class="p">,</span><span class="n">client_hostname</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="n">client_port</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
		<span class="n">pthread_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tid</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="kr">thread</span><span class="p">,</span><span class="n">connfdp</span><span class="p">);</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;name[%s] port[%s]connected! Thread id:%ld will serve it!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span>
					<span class="n">client_hostname</span><span class="p">,</span><span class="n">client_port</span><span class="p">,(</span><span class="kt">unsigned</span> <span class="kt">long</span> <span class="kt">long</span><span class="p">)</span><span class="n">tid</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="o">*</span><span class="nf">thread</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span> <span class="n">vargp</span><span class="p">){</span>
	<span class="kt">int</span> <span class="n">connfd</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">int</span> <span class="o">*</span><span class="p">)</span><span class="n">vargp</span><span class="p">);</span>
	<span class="n">pthread_detach</span><span class="p">(</span><span class="n">pthread_self</span><span class="p">());</span>
	<span class="n">free</span><span class="p">(</span><span class="n">vargp</span><span class="p">);</span>
	<span class="n">echo</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
	<span class="n">close</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
	<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">echo</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">)</span>
<span class="p">{</span>
	<span class="n">size_t</span> <span class="n">n</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>

	<span class="k">while</span><span class="p">((</span><span class="n">n</span><span class="o">=</span><span class="n">read</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">))</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;server recived %d bytes from fd %d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">n</span><span class="p">,</span><span class="n">connfd</span><span class="p">);</span>
		<span class="n">write</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="基于线程的事件驱动程序">基于线程的事件驱动程序</h2>
<p>也称作预线程化的方法</p>
<ul>
<li>主线程将连接符connfd放入池中</li>
<li>工作线程将connfd取出并处理</li>
<li>加入线程和取出线程都使用互斥量保护</li>
<li>echo程序中的cnt也使用互斥量保护</li>
</ul>
<p>server</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoserver_pre_thread 6667
client[localhost:38376] connected! connfd 4!                 <br>
server recived 7 (7 total) bytes on fd 4                     <br>
client[localhost:38380] connected! connfd 5!                 <br>
server recived 9 (16 total) bytes on fd 5                    <br>
server recived 4 (20 total) bytes on fd 4</p>
</blockquote>
<p>client1</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
123456
123456
123
123</p>
</blockquote>
<p>client2</p>
<blockquote>
<p>[root@linuxkit-00155ddc0103 inet]# ./echoclient localhost 6667
87654321
87654321</p>
</blockquote>
<p>sbuf.c fd池，插入，取出操作</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#include</span> <span class="cpf">&#34;sbuf.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="kt">void</span> <span class="nf">sbuf_init</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n</span><span class="p">){</span>
	<span class="n">sp</span><span class="o">-&gt;</span><span class="n">buf</span> <span class="o">=</span> <span class="n">calloc</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>
	<span class="n">sp</span><span class="o">-&gt;</span><span class="n">n</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span>
	<span class="n">sp</span><span class="o">-&gt;</span><span class="n">front</span> <span class="o">=</span> <span class="n">sp</span><span class="o">-&gt;</span><span class="n">rear</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
	<span class="n">sem_init</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">mutex</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
	<span class="n">sem_init</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">slots</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
	<span class="n">sem_init</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">items</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">snuf_deinit</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">){</span>
	<span class="n">free</span><span class="p">(</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">buf</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">sbuf_insert</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">,</span> <span class="kt">int</span> <span class="n">item</span><span class="p">){</span>
	<span class="n">sem_wait</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">slots</span><span class="p">);</span>
	<span class="n">sem_wait</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">mutex</span><span class="p">);</span>
	<span class="n">sp</span><span class="o">-&gt;</span><span class="n">buf</span><span class="p">[(</span><span class="o">++</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">rear</span><span class="p">)</span><span class="o">%</span><span class="p">(</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">n</span><span class="p">)]</span> <span class="o">=</span> <span class="n">item</span><span class="p">;</span>
	<span class="n">sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">mutex</span><span class="p">);</span>
	<span class="n">sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">items</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">sbuf_remove</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">){</span>
	<span class="kt">int</span> <span class="n">item</span><span class="p">;</span>
	<span class="n">sem_wait</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">items</span><span class="p">);</span>
	<span class="n">sem_wait</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">mutex</span><span class="p">);</span>
	<span class="n">item</span> <span class="o">=</span> <span class="n">sp</span><span class="o">-&gt;</span><span class="n">buf</span><span class="p">[(</span><span class="o">++</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">front</span><span class="p">)</span><span class="o">%</span><span class="p">(</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">n</span><span class="p">)];</span>
	<span class="n">sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">mutex</span><span class="p">);</span>
	<span class="n">sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sp</span><span class="o">-&gt;</span><span class="n">slots</span><span class="p">);</span>
	<span class="k">return</span> <span class="n">item</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>sbuf结构</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#ifndef SBUF_H_
</span><span class="cp">#define SBUF_H_
</span><span class="cp">#include</span> <span class="cpf">&#34;mynet.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;semaphore.h&gt;</span><span class="cp">
</span><span class="cp"></span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span>
	<span class="kt">int</span> <span class="o">*</span><span class="n">buf</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">n</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">front</span><span class="p">;</span>
	<span class="kt">int</span> <span class="n">rear</span><span class="p">;</span>
	<span class="n">sem_t</span> <span class="n">mutex</span><span class="p">;</span>
	<span class="n">sem_t</span> <span class="n">slots</span><span class="p">;</span>
	<span class="n">sem_t</span> <span class="n">items</span><span class="p">;</span>
<span class="p">}</span><span class="n">sbuf_t</span><span class="p">;</span>

<span class="kt">void</span> <span class="nf">dbuf_init</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">sbuf_deinit</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">p</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">sbuf_insert</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">,</span> <span class="kt">int</span> <span class="n">item</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">sbuf_remove</span><span class="p">(</span><span class="n">sbuf_t</span> <span class="o">*</span><span class="n">sp</span><span class="p">);</span>
<span class="cp">#endif
</span></code></pre></td></tr></table>
</div>
</div><p>echo_cnt.c</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#include</span> <span class="cpf">&#34;echo_cnt.h&#34;</span><span class="cp">
</span><span class="cp"></span>
<span class="k">static</span> <span class="kt">int</span> <span class="n">byte_cnt</span><span class="p">;</span>
<span class="k">static</span> <span class="n">sem_t</span> <span class="n">mutex</span><span class="p">;</span>

<span class="k">static</span> <span class="kt">void</span> <span class="nf">init_echo_cnt</span><span class="p">(</span><span class="kt">void</span><span class="p">){</span>
	<span class="n">sem_init</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mutex</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
	<span class="n">byte_cnt</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">echo_cnt</span><span class="p">(</span><span class="kt">int</span> <span class="n">connfd</span><span class="p">){</span>
	<span class="kt">int</span> <span class="n">n</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>
	<span class="k">static</span> <span class="n">pthread_once_t</span> <span class="n">once</span> <span class="o">=</span> <span class="n">PTHREAD_ONCE_INIT</span><span class="p">;</span>

	<span class="n">pthread_once</span><span class="p">(</span><span class="o">&amp;</span><span class="n">once</span><span class="p">,</span> <span class="n">init_echo_cnt</span><span class="p">);</span>
	<span class="k">while</span><span class="p">((</span><span class="n">n</span><span class="o">=</span><span class="n">read</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">))</span><span class="o">!=</span><span class="mi">0</span><span class="p">){</span>
		<span class="n">sem_wait</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mutex</span><span class="p">);</span>
		<span class="n">byte_cnt</span> <span class="o">+=</span> <span class="n">n</span><span class="p">;</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;server recived %d (%d total) bytes on fd %d</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span>
					<span class="n">n</span><span class="p">,</span><span class="n">byte_cnt</span><span class="p">,</span><span class="n">connfd</span><span class="p">);</span>
		<span class="n">sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mutex</span><span class="p">);</span>
		<span class="n">write</span><span class="p">(</span><span class="n">connfd</span><span class="p">,</span><span class="n">buf</span><span class="p">,</span><span class="n">n</span><span class="p">);</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>主程序</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-c" data-lang="c"><span class="cp">#include</span> <span class="cpf">&#34;mynet.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;sbuf.h&#34;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&#34;echo_cnt.h&#34;</span><span class="cp">
</span><span class="cp">#define NTHREADS 4
</span><span class="cp">#define SBUFSIZE 16
</span><span class="cp"></span>
<span class="kt">int</span> <span class="nf">open_listenfd</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">port</span><span class="p">);</span>
<span class="kt">void</span> <span class="o">*</span><span class="nf">thread</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">vargp</span><span class="p">);</span>

<span class="n">sbuf_t</span> <span class="n">sbuf</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
	<span class="kt">int</span> <span class="n">listenfd</span><span class="p">,</span> <span class="n">connfd</span><span class="p">,</span> <span class="n">i</span><span class="p">;</span>
	<span class="n">socklen_t</span> <span class="n">clientlen</span><span class="p">;</span>
	<span class="n">pthread_t</span> <span class="n">tid</span><span class="p">;</span>
	<span class="k">struct</span> <span class="n">sockaddr_storage</span> <span class="n">clientaddr</span><span class="p">;</span>
	<span class="kt">char</span> <span class="n">client_hostname</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">],</span> <span class="n">client_port</span><span class="p">[</span><span class="n">MAXLINE</span><span class="p">];</span>

	<span class="k">if</span><span class="p">(</span><span class="n">argc</span><span class="o">!=</span><span class="mi">2</span><span class="p">){</span>
	  <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="s">&#34;usage: %s &lt;port&gt;</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
	  <span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">listenfd</span> <span class="o">=</span> <span class="n">open_listenfd</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
	<span class="n">sbuf_init</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sbuf</span><span class="p">,</span><span class="n">SBUFSIZE</span><span class="p">);</span>
	<span class="k">for</span><span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="n">NTHREADS</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
		<span class="n">pthread_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tid</span><span class="p">,</span><span class="nb">NULL</span><span class="p">,</span><span class="kr">thread</span><span class="p">,</span><span class="nb">NULL</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
		<span class="n">clientlen</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_storage</span><span class="p">);</span>
		<span class="n">connfd</span> <span class="o">=</span> <span class="n">accept</span><span class="p">(</span><span class="n">listenfd</span><span class="p">,</span> <span class="p">(</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="o">&amp;</span><span class="n">clientlen</span><span class="p">);</span>
        <span class="n">getnameinfo</span><span class="p">((</span><span class="n">SA</span><span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">clientaddr</span><span class="p">,</span><span class="n">clientlen</span><span class="p">,</span><span class="n">client_hostname</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="n">client_port</span><span class="p">,</span><span class="n">MAXLINE</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
		<span class="n">sbuf_insert</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sbuf</span><span class="p">,</span><span class="n">connfd</span><span class="p">);</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">&#34;client[%s:%s] connected! connfd %d!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span>
					<span class="n">client_hostname</span><span class="p">,</span><span class="n">client_port</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">connfd</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="o">*</span><span class="nf">thread</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span> <span class="n">vargp</span><span class="p">){</span>
	<span class="n">pthread_detach</span><span class="p">(</span><span class="n">pthread_self</span><span class="p">());</span>
	<span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">){</span>
		<span class="kt">int</span> <span class="n">connfd</span> <span class="o">=</span> <span class="n">sbuf_remove</span><span class="p">(</span><span class="o">&amp;</span><span class="n">sbuf</span><span class="p">);</span>
		<span class="n">echo_cnt</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
	    <span class="n">close</span><span class="p">(</span><span class="n">connfd</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">Author</span>
    <span class="item-content">lclei</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">LastMod</span>
    <span class="item-content">
        2019-12-10
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">linux</a>
          <a href="/tags/net/">net</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/webserver/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">web服务器</span>
            <span class="prev-text nav-mobile">Prev</span>
          </a>
        <a class="next" href="/post/allocator/">
            <span class="next-text nav-default">allocator</span>
            <span class="next-text nav-mobile">Next</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:lclei@mail.ustc.edu.cn" class="iconfont icon-email" title="email"></a>
      <a href="https://gitee.com/lclei" class="iconfont icon-gitee" title="gitee"></a>
      <a href="https://gitee.com/lclei" class="iconfont icon-github" title="github"></a>
  <a href="http://localhost:1313/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://gohugo.io">Hugo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2018 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span>lclei</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c12618f9a600c40bd024996677e951e64d3487006775aeb22e200c990006c5c7.js"></script>








</body>
</html>
